/*
 * w5500test.c
 *
 *  Created on: Jun 30, 2025
 *      Author: CHENL
 */
#include "w5500test.h"
#include "gpio.h"
#include "main.h"
#include "wizchip_conf.h"
#include "main.h"
#include <stdio.h>
#include <string.h>
uint8_t  gDATABUF[16384]__attribute__((section(".w5500_cache")));
static uint8_t SSIZE[8]= {4,4,4,4,0,0,0,0}; /**< Max Tx buffer size by each channel */
static uint8_t RSIZE[8]= {4,4,4,4,0,0,0,0}; /**< Max Rx buffer size by each channel */

static void w5500_hard_rst(void)
{
    HAL_GPIO_WritePin(W5500_RESET_GPIO_Port, W5500_RESET_Pin, GPIO_PIN_RESET);
    HAL_Delay(2);
    HAL_GPIO_WritePin(W5500_RESET_GPIO_Port, W5500_RESET_Pin, GPIO_PIN_SET);
    HAL_Delay(10);
}

static int w5500_chip_init(void)
{
    /* default size is 2KB */
    //return wizchip_init(NULL, NULL);
        return wizchip_init(SSIZE, RSIZE);
}

static void w5500_phy_init(void)
{
    wiz_PhyConf conf;

    conf.by = PHY_CONFBY_SW;        //1
    conf.mode = PHY_MODE_MANUAL;    //0
    conf.speed = PHY_SPEED_100;        //1
    conf.duplex = PHY_DUPLEX_FULL;    //1

    wizphy_setphyconf(&conf);
    HAL_Delay(10);


    wiz_PhyConf phyconf;
    ctlwizchip(CW_GET_PHYCONF, (void*)&phyconf);
    if(memcmp(&conf,&phyconf,sizeof(wiz_PhyConf)) == 0){
      printf("set phy parameters success\r\n");// 配置成功
    }else{
      printf("set phy parameters fail\r\n"); // 配置失败
    }
    printf("by =%d;mode =%d;speed =%d;duplex =%d\n",phyconf.by,phyconf.mode,phyconf.speed,phyconf.duplex);

    uint8_t phylink;
    uint8_t phymode;
    ctlwizchip(CW_GET_PHYPOWMODE, (void*)&phymode);//PHY_POWER_NORM = 0,PHY_POWER_DOWN = 1
    printf("phymode = %d\n",phymode);
    ctlwizchip(CW_GET_PHYLINK, (void*)&phylink);//PHY_LINK_OFF = 0,PHY_LINK_ON = 1
    printf("phylink = %d\n",phylink);

    uint8_t version;
    version = getVERSIONR();
    printf("version = %d\n",version);

}

static void w5500_network_info_init(void)
{
    wiz_NetInfo info;

    uint8_t mac[6] = {0x00,0xf1,0xbe,0xc4,0xa1,0x05};
    uint8_t ip[4] = {192,168,2,100};
    uint8_t sn[4] = {255,255,255,0};
    uint8_t gw[4] = {192,168,2,1};
    uint8_t dns[4] = {0,0,0,0};

    memcpy(info.mac, mac, 6);
    memcpy(info.ip, ip, 4);
    memcpy(info.sn, sn, 4);
    memcpy(info.gw, gw, 4);
    memcpy(info.dns, dns, 4);

    info.dhcp = NETINFO_STATIC;

    wizchip_setnetinfo(&info);
}

void w5500_network_info_show(void)
{
    wiz_NetInfo info;

    wizchip_getnetinfo(&info);

    printf("w5500 network infomation:\r\n");
    printf("  -mac:%x:%x:%x:%x:%x:%x\r\n", info.mac[0], info.mac[1], info.mac[2],
            info.mac[3], info.mac[4], info.mac[5]);
    printf("  -ip:%d.%d.%d.%d\r\n", info.ip[0], info.ip[1], info.ip[2], info.ip[3]);
    printf("  -sn:%d.%d.%d.%d\r\n", info.sn[0], info.sn[1], info.sn[2], info.sn[3]);
    printf("  -gw:%d.%d.%d.%d\r\n", info.gw[0], info.gw[1], info.gw[2], info.gw[3]);
    printf("  -dns:%d.%d.%d.%d\r\n", info.dns[0], info.dns[1], info.dns[2], info.dns[3]);

    if (info.dhcp == NETINFO_DHCP) {
        printf("  -dhcp_mode: dhcp\r\n");
    } else {
        printf("  -dhcp_mode: static\r\n");
    }
}

int w5500_init(void)
{
    w5500_hard_rst();
     /* Register spi driver function */
    w5500_regFunc();

    /* socket buffer size init */
    if (w5500_chip_init() != 0) {
        return -1;
    }

    /* phy init */
    w5500_phy_init();

    /* network infomation init */
    w5500_network_info_init();

    /* show network infomation */
    w5500_network_info_show();

    return 0;
}

